Data Table

Row

Data Table

Interactive Graph

Row

Scatter Plot (PPG)

---
title: "NBA Betting Dashboard"
output: 
  flexdashboard::flex_dashboard:
    theme:
      version: 4
      bootswatch: default
      navbar-bg: "#3b5998"
    orientation: columns
    vertical_layout: fill
    source_code: embed
---
<style>
.chart-title 
  {  
    /* chart_title  */
    font-size: 18px;
    font-family: Arial;
  }
body
  {
      /* Normal  */
      font-size: 16px;
  }
</style>

```{r setup, include=FALSE}
library(flexdashboard)
```

```{r packages}
pacman::p_load(rvest, tidyverse, ggplot2, stringr, esquisse, plotly)
```

```{r scraping & cleaning (salary)}
link1 = "https://hoopshype.com/salaries/players/"
page1 = read_html(link1)

name = page1 %>%
  html_nodes("tbody .name") %>% html_text()
salary = page1 %>%
  html_nodes("tbody .hh-salaries-sorted") %>% html_text()

name <- trimws(trimws(name, "left", "\\w"), "right", "\n")
name <- trimws(trimws(name, "left", "\\w"), "right", "\t")
name <- trimws(trimws(name, "left", "\\w"), "left", "\n")
name <- trimws(trimws(name, "left", "\\w"), "left", "\t")

salary <- trimws(trimws(salary, "left", "\\w"), "right", "\t")
salary <- trimws(trimws(salary, "left", "\\w"), "left", "\n")
salary <- trimws(trimws(salary, "left", "\\w"), "left", "\t")
salary <- trimws(trimws(salary, "left", "\\w"), "left", "\\$")

nba_salary <- data.frame(name, salary)

salary <- gsub(",", "", nba_salary$salary)

nba_salary$salary <- salary

nba_salary$salary <- as.numeric(nba_salary$salary)
```

```{r scraping & cleaning (stats)}
nba_stats = data.frame()

for (pages in c(1,2,3)) 
  {
    link2 = paste0("https://basketball.realgm.com/nba/stats/2023/Averages/Qualified/points/All/desc/", pages, "/Regular_Season")  
    page2 = read_html(link2)
    
name = page2 %>% 
  html_nodes(".nowrap") %>% html_text()
    
team = page2 %>% 
  html_nodes(".nowrap+ td") %>% html_text()

gp = page2 %>% 
  html_nodes("td:nth-child(4)") %>% html_text()

mpg = page2 %>% 
  html_nodes("td:nth-child(5)") %>% html_text()

ppg = page2 %>% 
  html_nodes("td:nth-child(6)") %>% html_text()

fgm = page2 %>% 
  html_nodes("td:nth-child(7)") %>% html_text()

fga = page2 %>% 
  html_nodes("td:nth-child(8)") %>% html_text()

fgp = page2 %>% 
  html_nodes("td:nth-child(9)") %>% html_text()

tpm = page2 %>% 
  html_nodes("td:nth-child(10)") %>% html_text()

tpa = page2 %>% 
  html_nodes("td:nth-child(11)") %>% html_text()

tpp = page2 %>% 
  html_nodes("td:nth-child(12)") %>% html_text()

ftm = page2 %>% 
  html_nodes("td:nth-child(13)") %>% html_text()

fta = page2 %>% 
  html_nodes("td:nth-child(14)") %>% html_text()

ftp = page2 %>% 
  html_nodes("td:nth-child(15)") %>% html_text()

orb = page2 %>%
  html_nodes("td:nth-child(16)") %>% html_text()

drb = page2 %>%
  html_nodes("td:nth-child(17)") %>% html_text()

rpg = page2 %>%
  html_nodes("td:nth-child(18)") %>% html_text()

apg = page2 %>%
  html_nodes("td:nth-child(19)") %>% html_text()

spg = page2 %>%
  html_nodes("td:nth-child(20)") %>% html_text()

bpg = page2 %>%
  html_nodes("td:nth-child(21)") %>% html_text()

tov = page2 %>%
  html_nodes("td:nth-child(22)") %>% html_text()

pf = page2 %>%
  html_nodes("td:nth-child(23)") %>% html_text()

nba_stats <- rbind(nba_stats, as.data.frame(cbind(name, team, gp, ppg, fgm, fga, fgp, tpm, tpa, tpp, ftm, fta, ftp, orb, drb, rpg, apg, spg, bpg, tov, pf)))
  }

nba_stats <- nba_stats %>% mutate_at(c("ppg", "fgm", "fga", 
                                       "fgp", "tpm", "tpa", 
                                       "tpp", "ftm", "fta", 
                                       "ftp", "orb", "drb", 
                                       "rpg", "apg", "spg", 
                                       "bpg", "tov", "pf"), as.numeric)
```

```{r merge data}
nba <- left_join(nba_stats, nba_salary, by = "name")
nba$team <- as.factor(nba$team)
```

Data Table
===

Row {data-width=650}
-----------------------------------------------------------------------

### Data Table

```{r}
DT::datatable(nba, rownames = FALSE, 
              options = list(columnDefs = list(list(className = 'dt-center', targets = 1:21))))
```

Interactive Graph
===

Row {data-width=650}
-----------------------------------------------------------------------

### Scatter Plot (PPG)

```{r}
# plot1 <- ggplot(nba, aes(x = salary, y = ppg, 
#                    text = paste0("Player: ", name, "\n", "PPG: ", ppg, "\n", "Salary: ", salary))) + 
#   geom_point(shape = "triangle") + theme_classic() + labs(title = "Title")
#
# font1 <- list(family = "Mono", size = 15, color = "black")
# label1 <- list(bgcolor = "#dfe3ee", font = font1)
#
# ggplotly(plot1, tooltip = "text") %>%
#   style(hoverlabel = label1) %>%
#   layout(font = font1)
```